/**
 * Window with scrollable client area.
 * 
 * @author Andrey Anisimov <hercules@inbox.ru>
 */

import core.ui.ScrollBar;
import core.ui.SplitWindow;
import core.ui.Window;

class ScrollWindow extends SplitWindow {

	/**
	 * Creates new scroll window.
	 * 
	 * @param parent (Window) [optional] parent window
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] window size
	 */	
	this.ScrollWindow = function(parent, x, y, width, height) {
		super(parent, x, y, width, height);
		this.vscroll = this.createScrollBar(0, 0, undefined, '100%');
		this.hscroll = this.createScrollBar(0, 0, '100%', undefined);
		this.hscroll.setOrientation(ScrollBar.HORISONTAL);
		this.addPane(this.hscroll, SplitWindow.SPLIT_BOTTOM);
		this.addPane(this.vscroll, SplitWindow.SPLIT_RIGHT);
		this.updateLayout();
	}
	
	/**
	 * Creates new scroll bar.
	 * 
	 * @param x, y (int) [optional] coordinates
	 * @param width, height (int) [optional] scroll bar size
	 */	
	this.createScrollBar = function(x, y, width, height) {
		return new ScrollBar(this, x, y, width, height);
	}

	/**
	 * Sets the text in scrolling area.
	 * 
	 * @param text (string) new text
	 */
	this.setText = function(text) {
		if (this.content instanceof Window) {
			this.content.setText(text);
			this.updateLayout();
		}
	}

	/**
	 * Returns current scrolling area text.
	 *
	 * @return string
	 */
	this.getText = function() {
		return (this.content instanceof Window)
			? this.content.getText() : '';
	}
	
	/**
	 * Ensures child component of content window is visible.
	 *
	 * @param child (Window) child window
	 */
	this.ensureVisible = function(child) {
		if ((this.content instanceof Window) && (child instanceof Window) && child.isChildOf(this.content)) {
			with (this.content.getElement()) {
				scrollLeft = child.getX();
				scrollTop = child.getY();
			}
			this.vscroll.updateLayout();
			this.hscroll.updateLayout();
		}
	}
	
	/**
	 * Sets the content window to scroll.
	 * 
	 * @param window (Window) scroll content area.
	 */
	this.setContentWindow = function(wnd) {
		wnd.setStyle('overflow', 'hidden');
		this.addPane(wnd, SplitWindow.SPLIT_FILL);
		this.vscroll.setScrollTarget(wnd);
		this.hscroll.setScrollTarget(wnd);
		this.content = wnd;
	}

	/**
	 * Returns content child window.
	 * Use it to create your GUI inside the scroll window.
	 *
	 * @return Window
	 */
	this.getContentWindow = function() {
		return this.content;
	}
	
	/**
	 * Returns vertical scroll bar control.
	 *
	 * @return ScrollBar
	 */
	this.getVerticalScroll = function() {
		return this.vscroll;
	}
	
	/**
	 * Returns horisontal scroll bar control.
	 *
	 * @return ScrollBar
	 */
	this.getHorisontalScroll = function() {
		return this.hscroll;
	}

	/**
	 * Updates child controls size and layout.
 	 */
	this.updateLayout = function() {
		if (this.content instanceof Window) {
			
			var w, h;
			// Show/hide scrollbars
			with (this.content.getElement()) {
				this.vscroll.setStyle('display', (scrollHeight > offsetHeight) ? 'block' : 'none');
				this.hscroll.setStyle('display', (scrollWidth > offsetWidth) ? 'block' : 'none');
				h = (scrollWidth > offsetWidth) ? (this.getClientHeight() - this.hscroll.getHeight()) : this.getClientHeight();
				w = (scrollHeight > offsetHeight) ? (this.getClientWidth() - this.vscroll.getWidth()) : this.getClientWidth();
			}
			
			// Resize scrollbars
			this.hscroll.setStyle('width', Math.max(w, 0) + 'px');
			this.vscroll.setStyle('height', Math.max(h, 0) + 'px');
			this.hscroll.updateLayout();
			this.vscroll.updateLayout();
		}

		super.updateLayout();
	}

	/**
	 * Resets scrollbars position.
	 * TODO: rename this method
	 */
	this.reset = function() {
		this.vscroll.setPosition(0);
		this.hscroll.setPosition(0);
		this.updateLayout();
	}
}
